<?php
                        

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class roomAndLevel
{
    public function __construct($dbConnection)
    {
        $this->Connection=$dbConnection;
    }
    
    public $SwitchPortID = null;
    public $SwitchPortName = null;
    public $SwitchPortChangeFlag = null;
    public $SwitchPortSpeed = null;
    
    public $SwitchID = null;
    public $SwitchName = null;
    public $SwitchIp = null;
    public $SwitchChangeFlag = null;
    public $SwitchNote = null;
    
    public $ID = null;
    public $PatchPanelName = null;
    
    public $RoomID = null;
    public $RoomName = null;
    public $RoomNumber = null;
    
    public $LevelID = null;
    public $LevelName = null;
    
    
    
    
    public function SwitchExistsOrCreate() {  
        $switches=$this->GetAllSwitch();
        $queries = new mysqlqueries();
        
        $ids = array();
        $names = array();
        foreach ($switches as $value) {
            array_push($ids, $value['ID']);
            array_push($names, $value['name']);
        }
        
        if(in_array($this->SwitchName, $names)){
            
            $this->SwitchID = $ids[array_search($this->SwitchName, $names)];
        }
        else{
            $upArray = array(NULL,$this->SwitchName, $this->SwitchIp, $this->SwitchChangeFlag, $this->SwitchNote);
            $this->Connection->query($queries->Add($queries->Switch,$upArray));
            $this->SwitchID = $this->Connection->lastID();
        }
    }
 
    public function RoomExistsOrCreate($update){
        
        $queries = new mysqlqueries();

        $tempRooms = $this->Connection->query($queries->GetIndex($queries->Room, array($this->RoomName, $this->RoomNumber), array('name', 'number'), NULL));
        
        if($this->LevelName != null)
        {
            $this->LevelExistsOrCreate();
        }
        
        if($tempRoom = mysqli_fetch_array($tempRooms))
        {
            $this->RoomID = $tempRoom['ID'];
            $this->RoomNumber = $tempRoom['number'];
            if($update)
              $this->UpdateRoom($this->LevelID, 'level_ID');
            return false;
        }else
        {
            $upArray = array(NULL,$this->RoomName,  $this->RoomNumber, $this->LevelID);
            $this->Connection->query($queries->Add($queries->Room,$upArray));
            $this->RoomID = $this->Connection->lastID();
            return true;
        }
    }

    public function LevelExistsOrCreate(){
        $queries = new mysqlqueries();
        $resultLevel = $this->Connection->query($queries->GetIndex($queries->Level, $this->LevelName, 'name', null));
        if($rowLevel= mysqli_fetch_array($resultLevel))
        {
            $this->LevelID = $rowLevel['ID'];
            return false;
        }else
        {
            $upArray = array(NULL, $this->LevelName);
            $this->Connection->query($queries->Add($queries->Level,$upArray));
            $this->LevelID = $this->Connection->lastID();
            return true;
        }   
    }
    
    public function SwitchportExistsOrCreate()
   {
        $queries = new mysqlqueries();
        $resultSwitchport = $this->Connection->query($queries->GetIndex($queries->Switchport, array($this->SwitchPortName, $this->SwitchID),array('name', 'switch_ID'), null));
        if($rowSwitchport= mysqli_fetch_array($resultSwitchport))
        {
            $this->SwitchPortID = $rowSwitchport['ID'];
            $this->SwitchPortChangeFlag = $rowSwitchport['changeFlag'];
            $this->SwitchPortSpeed = $rowSwitchport['speed'];
        }else
        {
            $upArray = array($this->SwitchPortName, $this->SwitchPortSpeed, $this->SwitchPortChangeFlag, $this->SwitchID, null);
            $this->Connection->query($queries->Add($queries->Switchport,$upArray));
            $this->SwitchPortID = $this->Connection->lastID();
        }
    }  
    public function PatchpannelEntryExistsOrCreate()
    {
       $queries = new mysqlqueries();
        $resultPatchpannel = $this->Connection->query($queries->GetIndex($queries->Patchpanel, $this->PatchPanelName, 'name', null)); 
        if($rowPatchpannel= mysqli_fetch_array($resultPatchpannel))
        {
            $this->ID = $rowPatchpannel['ID'];
            $this->PatchPanelName = $rowPatchpannel['name'];
            $this->UpdateRoomAndLevel();
        }else
        {
            $this->CreateRoomAndLevel();
        }
           
        
    }

    public function AllExistsOrCreate(){
        $this->SwitchExistsOrCreate();
        $this->RoomExistsOrCreate(true);
        $this->SwitchportExistsOrCreate();
        $this->PatchpannelEntryExistsOrCreate();
    }
    
    
    
    public function GetAllSwitch(){
        $queries = new mysqlqueries();
        $resultLevel = $this->Connection->query($queries->GetTable($queries->Switch, "name"));
        $arr = array();
        while ($row = mysqli_fetch_array($resultLevel)){
            array_push($arr, $row);
        }
        return $arr;
    }
    
    public function GetAllRoom(){
        $queries = new mysqlqueries();
        $resultLevel = $this->Connection->query($queries->GetTable($queries->Room, "name"));
        $arr = array();
        while ($row = mysqli_fetch_array($resultLevel)){
            array_push($arr, $row);
        }
        return $arr;
    }
    
    public function getAllRoomsPerLevel ($level) {
        $queries = new mysqlqueries();
        $resultRoom = $this->Connection->query($queries->GetIndex($queries->Room, $level, "level_ID", "name"));
        $arr = array();
        while ($row = mysqli_fetch_array($resultRoom)) {
            array_push($arr, $row);
        }
        return $arr;
    }
    
    public function getAllLevel() {
        $queries = new mysqlqueries();
        $resultLevel = $this->Connection->query($queries->GetTable($queries->Level, null));
        $arr = array();
        while ($row = mysqli_fetch_array($resultLevel)){
            array_push($arr, $row);
        }
        return $arr;
    }
    
    public function GetAllSwitchPorts()
    {
        $queries = new mysqlqueries();
        $resultLevel = $this->Connection->query($queries->GetTable($queries->Switchport, 'name'));
        $arr = array();
        while ($row = mysqli_fetch_array($resultLevel)){
            array_push($arr, $row);
        }
        return $arr;  
    }
        
   
    public function getInfo()
    {
        if($this->ID == null)
        {
            return false;
        }
        $queries = new mysqlqueries();
        $patchpannelEntry = $this->Connection->query($queries->GetIndex($queries->Patchpanel, $this->ID, null, null));
        
        if($rowPatchpannelEntry = mysqli_fetch_array($patchpannelEntry)) 
        {      
            $this->SwitchID = $rowPatchpannelEntry['switchPort_switch_ID'];
            $this->SwitchPortID = $rowPatchpannelEntry['switchPort_ID'];
            $this->RoomID = $rowPatchpannelEntry['room_ID'];
            $this->PatchPanelName = $rowPatchpannelEntry['name'];
            
            $resultSwitch = $this->Connection->query($queries->GetIndex($queries->Switch, $this->SwitchID, null, null));
            $resultSwitchport = $this->Connection->query($queries->GetIndex($queries->Switchport, $this->SwitchPortID, null, null));
            
            if($rowSwitch = mysqli_fetch_array($resultSwitch))
            {
                    $this->SwitchName = $rowSwitch['name'];
                    $this->SwitchIp = $rowSwitch['ip'];
                    $this->SwitchChangeFlag = $rowSwitch['changeFlag'];
                    $this->SwitchNote = $rowSwitch['note'];
            }
            if($rowSwitchport = mysqli_fetch_array($resultSwitchport))
            {
                $this->SwitchPortName = $rowSwitchport['name'];
                $this->SwitchPortChangeFlag = $rowSwitchport['changeFlag'];
                $this->SwitchPortSpeed = $rowSwitchport['speed'];
            }
            
            $this->getRoomInfo();         
            return true;     
        }
        
    }
    
    public function getRoomInfo()
    {      
        if($this->RoomID == null)
        {
            return false;
        }
        $queries = new mysqlqueries();
        $resultRoom = $this->Connection->query($queries->GetIndex($queries->Room, $this->RoomID, null, null));
        if($rowRoom = mysqli_fetch_array($resultRoom))
        {
            $this->RoomName = $rowRoom['name'];
            $this->RoomNumber = $rowRoom['number']; 
            $this->LevelID = $rowRoom['level_ID'];;
            $resultLevel = $this->Connection->query($queries->GetIndex($queries->Level, $this->LevelID, null, null));
            if($rowLevel= mysqli_fetch_array($resultLevel))
            {
                $this->LevelName = $rowLevel['name'];
            }
        } 
        return true;
    }
    
    public function getAllPatchpanelIDs()
    {
         $patchPannelIDs = array();
         $queries = new mysqlqueries();

         $patchpannelEntries = $this->Connection->query($queries->GetTable($queries->Patchpanel,  'switchPort_switch_ID'));
         
         while ($rowPatchpannelEntries = mysqli_fetch_array($patchpannelEntries))
         {
             array_push($patchPannelIDs, $rowPatchpannelEntries['ID']);
         }
         return $patchPannelIDs;     
    }
    
    
    
    public function UpdatePatchPannel($patchpannelValues, $patchpannelColumn)
    {
        $queries = new mysqlqueries();
        
        $this->Connection->query($queries->Update($queries->Patchpanel, $patchpannelValues, $this->ID, $patchpannelColumn, null));
        return true;
    }
   
    public function UpdateSwitch($swichValues, $switchColumn) 
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Update($queries->Switch, $swichValues, $this->SwitchID, $switchColumn, null));
        return true;
    }
   
    public function UpdateSwitchPort($switchPortValues, $switchPortColumn) 
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Update($queries->Switchport, $switchPortValues, $this->SwitchPortID, $switchPortColumn, null));
        return true;
    }
   
    public function UpdateRoom($roomValues, $roomColumn) 
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Update($queries->Room, $roomValues, $this->RoomID, $roomColumn, null));
        return true;
    }
  
    public function UpdateLevel($levelValues, $levelColumn)
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Update($queries->Level, $levelValues, $this->LevelID, $levelColumn, null));
        return true;
    }
    
    public function UpdateRoomAndLevel()
    {
        $this->UpdatePatchPannel(array($this->PatchPanelName, $this->SwitchPortID, $this->SwitchID, $this->RoomID), array('name', 'switchPort_ID', 'switchPort_switch_ID', 'room_ID'));
    }
    
    
    
    public function CreateRoomAndLevel()
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Add($queries->Patchpanel, array($this->PatchPanelName, $this->SwitchPortID, $this->SwitchID, $this->RoomID)));
        $this->ID = $this->Connection->lastID();
    }  
        
    public function CheckIfRoomIsInUse() 
    {
        $result =true; 
        $queries = new mysqlqueries();
        $temPatchpanel = $this->Connection->query($queries->GetIndex($queries->Patchpanel, $this->RoomID, 'room_ID', null));
        $temRoomIs = $this->Connection->query($queries->GetIndex($queries->Relocation, $this->RoomID, 'roomIs_ID', null));
        $temRoomShould = $this->Connection->query($queries->GetIndex($queries->Relocation, $this->RoomID, 'roomShould_ID', null));
        if(mysqli_fetch_array($temPatchpanel))
        {
           $result = false; 
        }
        if(mysqli_fetch_array($temRoomIs))
        {
           $result = false; 
        }
        if(mysqli_fetch_array($temRoomShould))
        {
            $result = false; 
        }
        return $result;
    }
    
    public function CheckIfLevelIsInUse()
    {
        $result =false; 
        $queries = new mysqlqueries();
        $tempRoom = $this->Connection->query($queries->GetIndex($queries->Room, $this->LevelID, 'level_ID', null));
         if(!mysqli_fetch_array($tempRoom))
        {
            $result = true; 
        }
        return $result;
    }
    
    public function CheckIfSwitchportIsInUse()
    {
        $result =false; 
        $queries = new mysqlqueries();
        $tempRoom = $this->Connection->query($queries->GetIndex($queries->Mac, $this->SwitchPortID, 'switchPort_ID', null));
         if(!mysqli_fetch_array($tempRoom))
        {
            $result = true; 
        }
        return $result;
    }
  
    
    
    public function DeletePatchPanel()
    {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Remove($queries->Patchpanel, null, $this->ID));
        return true;
    }
      
    public function DeleteSwitchPort() 
    {
        if($this->CheckIfSwitchportIsInUse())
        {
        $queries = new mysqlqueries();
        $this->Connection->query($queries->Remove($queries->Switchport, null, $this->SwitchPortID));
        return true;
        }else
        {
            return false; 
        }
    }
    
    public function DeleteLevel()
    {
        if($this->CheckIfLevelIsInUse())
        {
            $queries = new mysqlqueries();
            $this->Connection->query($queries->Remove($queries->Level, null, $this->LevelID));
            return true;
        }else
        {
            return false; 
        }
    }   
    
    public function DeleteRoom()
    {
        if($this->CheckIfRoomIsInUse())
        {
            $queries = new mysqlqueries();
            $this->Connection->query($queries->Remove($queries->Room, null, $this->RoomID));
            return true;
        }else
        {
            return false;
        }
    }
}




?>
